import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/components/NetWorkImage/NetworkImageWidget.dart';
import 'package:sinosheets/route/route_utils.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

import 'logic.dart';

class NormalCategoryPListPage extends StatefulWidget {
  const NormalCategoryPListPage({Key? key}) : super(key: key);

  @override
  State<NormalCategoryPListPage> createState() => _NormalCategoryPListPageState();
}

class _NormalCategoryPListPageState extends State<NormalCategoryPListPage> {
  final NormalCategoryPListLogic logic = Get.put(NormalCategoryPListLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<NormalCategoryPListLogic>(builder: (get) {
      return Scaffold(
        appBar: AppBar(
          title: Text(get.categoryName),
          centerTitle: true,
          backgroundColor: Colors.white,
          elevation: 0,
        ),
        body: GridView.builder(
          shrinkWrap: true,
          padding: EdgeInsets.symmetric(horizontal: 16.r),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            mainAxisSpacing: 16.r,
            crossAxisSpacing: 16.r,
            childAspectRatio: 0.65,
          ),
          itemCount: get.productList.length,
          itemBuilder: (context, index) {
            return _buildProductCard(get.productList[index], get);
          },
        ),
      );
    });
  }

  /// 构建商品卡片
  Widget _buildProductCard(Map<String, dynamic> product, NormalCategoryPListLogic get) {
    return GestureDetector(
      onTap: () {
        RouteUtils.toProductDetail(product);
      },
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12.r),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              blurRadius: 8.r,
              offset: Offset(0, 2.r),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Center(
              child: ClipRRect(
                borderRadius: BorderRadius.vertical(top: Radius.circular(12.r)),
                child: NetworkImageWidget(
                  url: product['img'],
                  width: double.infinity,
                  height: 160.r,
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Padding(
                padding: EdgeInsets.symmetric(horizontal: 10.r),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(height: 12.r),
                    Text(
                      product['itemName'] ?? '',
                      style: TextStyle(fontSize: 13.r, color: Color(0xFF333333)),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    SizedBox(height: 8.r),
                    Row(
                      children: [
                        Text('${product['price']} ${product['symbol']}', style: TextStyle(color: Color(0xFFFF7D00), fontSize: 14.r, fontWeight: FontWeight.bold)),
                        if (product['crossedPrice'] != product['price'])
                          Row(
                            children: [
                              SizedBox(width: 6.r),
                              Text('${product['crossedPrice']} ${product['symbol']}', style: TextStyle(color: ColorUtils.c_666666, fontSize: 10.r, decoration: TextDecoration.lineThrough)),
                            ],
                          )
                      ],
                    ),
                    SizedBox(height: 10.r),
                  ],
                ))
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<NormalCategoryPListLogic>();
    super.dispose();
  }
}
